/**
 * Copyright (C) 2012  CFutureAndroidSupport
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lurencun.android.support.v2.ui;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

import com.lurencun.android.sdk.res.AssetsReader;
import com.lurencun.android.sdk.util.BitmapScaleUitl;
import com.lurencun.android.support.ui.BaseActivity;
import com.lurencun.android.support.ui.LightDialog;
import com.lurencun.android.support.v2.widget.ScalableImageView;
import com.lurencun.android.support.widget.CommonAdapter;
import com.lurencun.android.support.widget.GalleryFlipper;

/**
 * 
 * @author cfuture.chenyoca [桥下一粒砂] (chenyoca@163.com)
 * @date 2012-3-4
 */
public abstract class GalleryActivity extends BaseActivity {
	
	private GalleryFlipper mGallery;
	
	private int screenHeight;
	private int screenWidth;
	
	@Override
	final protected void onCreate(Bundle savedInstanceState) {
		this.hideTitleBar();
		this.setScreenVertical();
		this.manualFullScreen(true);
		super.onCreate(savedInstanceState);
		
		mGallery = new GalleryFlipper(this);
		LayoutParams param = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		setContentView(mGallery,param);
		
		WindowManager manage=getWindowManager();
	    Display display=manage.getDefaultDisplay();
	    screenHeight=display.getHeight();
	    screenWidth=display.getWidth();
		
		CommonAdapter<String> adapter = new GalleryAdapter(this);
		
		onCreateEx();
		
		adapter.updateDataCache(getImagesPath());
		mGallery.setAdapter(adapter);
		
	}
	
	protected abstract void onCreateEx();
	protected abstract List<String> getImagesPath();
	
	
	@Override
	final public boolean onTouchEvent(MotionEvent event) {
		return mGallery.onFlipperTouchEvent(event);
	}


	private class GalleryAdapter extends CommonAdapter<String>{

		public GalleryAdapter(Context context) {
			super(context);
		}

		@Override
		protected View createView(LayoutInflater inflater, String data,
				int position, View convertView, ViewGroup parent) {
			RelativeLayout cell = new RelativeLayout(GalleryActivity.this);
			android.widget.RelativeLayout.LayoutParams param = 
					new android.widget.RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
			cell.setLayoutParams(param);
			
			ImageView view = new ImageView(mContext);
			final Bitmap image = AssetsReader.readBitmap(mContext, data);
			view.setImageBitmap(BitmapScaleUitl.prorateThumbnail(image, screenWidth, screenHeight));
			
			view.setOnTouchListener(new OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					LightDialog dialog = new LightDialog(mContext) {
						@Override
						protected void onCreateEx(Bundle savedInstanceState) {
							
						}
						@Override
						protected View getContentView() {
							ScalableImageView view = new ScalableImageView(mContext);
							view.setImageBitmap(image);
							return view;
						}
					};
					dialog.show();
					return false;
				}
			});
			
			cell.addView(view);
			return cell;
		}
		
	}
	
}
